import { _ as _sliced_to_array } from "@swc/helpers/_/_sliced_to_array";
import { KEYCONST } from "../enums";
import { useLatest } from "ahooks";
import { useEffect, useMemo, useRef, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
var allowPath = [
    "/login",
    "/nftDetails",
    "/otherLogin"
];
export default function useAuth() {
    var userInfo = useSelector(function(state) {
        return state.app.userInfo;
    });
    var path = useSelector(function(state) {
        return state.app.path;
    });
    /** 是否登录 */ var _useState = _sliced_to_array(useState(!!userInfo.id), 2), auth = _useState[0], setAuth = _useState[1];
    var authRef = useLatest(auth);
    var dispatch = useDispatch();
    var initPadding = useRef(false);
    var currentRouteQuote = useRef([]);
    var pathname = useMemo(function() {
        return typeof path === "object" ? path.path : path;
    }, [
        path
    ]);
    var lastPathname = useLatest(pathname);
    var hasAuth = useMemo(function() {
        return allowPath.includes(pathname) || auth;
    }, [
        pathname,
        auth
    ]);
    useEffect(function() {
        currentRouteQuote.current.push(pathname);
        var token = JSON.parse(localStorage.getItem(KEYCONST.token) || "{}").accessToken;
        if (!authRef.current) {
            if (token) {
                if (!initPadding.current) {
                    initPadding.current = true;
                    /** 请求前置数据 */ Promise.all([
                        dispatch.app.setUserInfo(),
                        dispatch.app.setCoinInfos()
                    ]).then(function(param) {
                        var _param = _sliced_to_array(param, 1), user = _param[0];
                        setAuth(true);
                        initPadding.current = false;
                        currentRouteQuote.current.splice(0, 1);
                    }).catch(function(err) {
                        initPadding.current = false;
                        // 请求发生错误
                        //   console.log('token 过期', pathname, currentRouteQuote.current,lastPathname.current);
                        if (!allowPath.includes(lastPathname.current)) {
                            // 获取本地是否存在token
                            dispatch.app.PUSH("/login");
                        }
                        currentRouteQuote.current.splice(0, 1);
                    });
                }
            } else {
                currentRouteQuote.current.splice(0, 1);
                if (!allowPath.includes(lastPathname.current)) {
                    dispatch.app.PUSH("/login");
                }
            }
        }
    }, [
        pathname
    ]);
    return hasAuth;
}

 //# sourceMappingURL=useAuth.js.map